package dbopr

import (
	"fmt"
	"time"

	"go.uber.org/zap"
)

type GormWinLogger struct{}
type GormZapLogger struct{}

var zapLogger GormZapLogger

func (GormWinLogger) Print(values ...interface{}) {
	level := values[0]

	currntTime := time.Now().Format("2006-01-02 15:04:05")
	out := currntTime + " "
	if level != "sql" {
		for _, value := range values[1:] {
			out += fmt.Sprintf("[%v] ", value)
		}
		out += "\n"
	} else {
		out += fmt.Sprintf("(%s) %s [%s] %v [%d rows affected or returned]\n", values[1], values[2], values[3], values[4], values[5])
	}
	fmt.Printf("%s\n", out)
}

func (GormZapLogger) Print(values ...interface{}) {
	if len(values) < 6 {
		logger.Warnf("gorm zap logger, too small arguments")
		return
	}

	category := values[0].(string)
	line := values[1].(string)
	latency := values[2].(time.Duration)
	sql := values[3].(string)
	vals := values[4]
	effect := values[5].(int64)

	logger.Desugar().Debug(sql,
		zap.String("line", line),
		zap.Int64("effect", effect),
		zap.String("category", category),
		zap.Duration("latency", latency),
		zap.Any("arguments", vals),
	)
}
